﻿using Confluent.Kafka;
using HealthChecks.Kubernetes;
using k8s;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MicroServiceHealthCheck.Config
{
    public static class HealthCheckConfigExtensions
    {
        public static void ConfigHealthCheck(this IServiceCollection services) 
        {
            IHealthChecksBuilder healthChecksBuilder = services.AddHealthChecks();
            // 监控kafka
            var config = new ProducerConfig { BootstrapServers = "47.114.42.211:19092" };
            healthChecksBuilder.AddKafka(new ProducerConfig(config), 
                name: "usercenter-kafka", 
                topic:"test-1",
                failureStatus: HealthStatus.Unhealthy, 
                tags: new[] { "usercenter-kafka" });

            // 监控rabbitmq
            healthChecksBuilder.AddRabbitMQ(
                rabbitConnectionString: "rabbitmq://115.28.33.225", 
                name: "mgscenter-rabbitmq",
                failureStatus: HealthStatus.Unhealthy,
                tags: new[] { "rabbitmq" });

            // 监控mysql
            healthChecksBuilder.AddMySql("Data Source=rm-bp11o6z22u80cec52eo.mysql.rds.aliyuncs.com;port=3306;Initial Catalog=usercenter2021_fat;user id=user_usercenter;password=Lunz2017;Charset=utf8;TreatTinyAsBoolean=false;AllowUserVariables=true;",
                name: "usercenter-mysql",
                failureStatus: HealthStatus.Unhealthy,
                tags: new[] { "usercenter-mysql" });

            // 监控IdentityServer
            healthChecksBuilder.AddIdentityServer(new Uri("http://identity-fat.lunz.cn"),
                name:"idsvr4", 
                failureStatus: HealthStatus.Unhealthy,
                tags: new[] { "idsvr4" });

            // 监控redis
            healthChecksBuilder.AddRedis("r-bp153cb6bc443ba4pd.redis.rds.aliyuncs.com:6379,allowAdmin=true",
                name:"whmp-redis",
                failureStatus: HealthStatus.Unhealthy,
                tags: new[] { "whmp-redis" });

            // 自定义url检查
            //healthChecksBuilder.AddUrlGroup(new Uri("http://httpbin.org/status/200"),
            //    name: "httpbin",
            //    failureStatus: HealthStatus.Unhealthy,
            //    tags: new[] { "httpbin" });
        }
    }
}
